#!/usr/bin/env bash
set -e
set -u
set -o pipefail


main() {
    local my_file=${1:?}
    local outfile=test_output
    export ENCODED_KEY=$(base64 ${my_file} -w 0)
    local ENCODED_KEY_1="${ENCODED_KEY:0:4000}"
    local ENCODED_KEY_N="${ENCODED_KEY:4000}"
    echo "${ENCODED_KEY_1}" | base64 --decode > ${outfile}
    echo "${ENCODED_KEY_N}" | base64 --decode >> ${outfile}
    diff ${outfile} ${my_file} && \
        echo "${ENCODED_KEY}" && \
        echo "${#ENCODED_KEY}" && \
        echo "" && echo "${ENCODED_KEY_1}" && \
        echo "" && echo "${ENCODED_KEY_N}" && \
        [ "${#ENCODED_KEY_N}" -lt 4000 ] || \
            echo "ERROR: Failed to encode and then decode the input"
}


main $@
